﻿/* Write a program that reads two numbers N and K and generates all the combinations of K distinct elements from the set [1..N]. 
 * Example:
	N = 5, K = 2  {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 3}, {2, 4}, {2, 5}, {3, 4}, {3, 5}, {4, 5}
*/




using System;

class Combinations
{
    static void Main()
    {
        int N, K;

        bool iProvided = false;
        do
        {
            if (iProvided == true) Console.WriteLine("Error occurred. Please enter an integer.");
            else iProvided = true;
            Console.Write("Enter N: ");
        } while (Int32.TryParse(Console.ReadLine(), out N) == false);

        iProvided = false; //reset iProvided
        do
        {
            if (iProvided == true) Console.WriteLine("Error occurred. Please enter an integer (oh, and K should be smaller than N)\r\nValid input: Integer && N>K");
            else iProvided = true;
            Console.Write("Enter K: ");
        } while (Int32.TryParse(Console.ReadLine(), out K) == false || K > N);

        int[] arr = new int[K];

        Calculate_combos(ref arr, 0, N, 1);
    }

    private static int Calculate_combos(ref int[] arr, int kparam, int nparam, int offset)
    {
        if (kparam == arr.Length)
        {
            PrintArray(arr); //print our array
            return 0; //do not execute anything else from this block, my task is only to print the current array state.
        }
        //alter our array, iterate through starting from <offset> to <nparam> - 
        /* for each <i> to <offset> change arr[kparam] to i, then call 
         * the method itself with kparam+1 (increasing kparam to reach the supposed/desired length, and
         * i+1 (our base number has increased)
         */
        for (int i = offset; i <= nparam; i++)
        {
            arr[kparam] = i;
            Calculate_combos(ref arr, kparam + 1, nparam, i + 1);
        }
        return 0;
    }

    private static void PrintArray(int[] arrParam)
    {
        string output = "{";
        for (int i = 0; i < arrParam.Length; i++) output += arrParam[i] + ", ";
        output = output.Substring(0, output.Length - 2); //make it beautiful (remove the last ", ")
        output += "}";
        Console.Write(output + ", ");
    }

}
